import 'package:flutter/material.dart';

class StarRating extends StatefulWidget {
  final double rating;       // 当前分数
  final double maxRating;    // 满分
  final int count;           // 总共几颗心
  final double size;          // 星星大小
  final Color defaultColor;
  final Color selectedColor;
  final Widget unselectedImage;
  final Widget selectedImage;

  const StarRating({
    required this.rating,   // 必填
    this.maxRating = 10,
    this.count = 5,
    this.size = 30,
    this.defaultColor = Colors.grey,
    this.selectedColor = Colors.red,
    required this.unselectedImage,
    required this.selectedImage,

//Icon(Icons.star_border, color: Colors.grey, size: size)
//   Icon(Icons.star, color: Colors.red,size: size)
  });

  @override
  _StarRatingState createState() => _StarRatingState();
}

class _StarRatingState extends State<StarRating> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Row(
            mainAxisSize: MainAxisSize.min,
            children: buildDefaultStar()
        ),
        Row(
            mainAxisSize: MainAxisSize.min,
            children: buildSelectedStar()
        )
      ],
    );
  }


  /*
  *  创建默认的星星
  * */
  List<Widget> buildDefaultStar(){
    return List.generate(widget.count, (index) {
      return widget.unselectedImage;
    });
  }

  /*
  *  创建选中的星星
  * */
  List<Widget> buildSelectedStar() {
    // 创建stats
    List<Widget> starts = [];
    final star = widget.selectedImage;
    //构建满的start
    double oneValue = widget.maxRating / widget.count;
    int entireCount = (widget.rating / oneValue).floor(); //.floor() 向下取整

    for (var i = 0; i< entireCount ; i++){
      starts.add(star);
    }
    //构建部分start
    double leftWidth = ((widget.rating /oneValue)-entireCount)* widget.size;
    final halfStar = ClipRect(
        clipper: HTStartClipper(leftWidth),
        child: star
    );
    starts.add(halfStar);

    // 防止传入参数大于总分
    if (starts.length > widget.count){
      return starts.sublist(0,widget.count);
    }

    return starts;
  }
}

/*
* 切割
* */
class HTStartClipper extends CustomClipper<Rect>{

  double width;
  HTStartClipper(this.width);

  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, width, size.height);
  }

  @override
  bool shouldReclip(HTStartClipper oldClipper) {
    return oldClipper.width != this.width;
  }
}